package com.ykt.common.config;

import java.util.Map;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.kit.ResourceKit;
import com.jfinal.ext.plugin.cron.Cron4jPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.ykt.interceptor.AuthorityIntercept;
import com.ykt.interceptor.LoginAdminInterceptor;
import com.ykt.model.Ad;
import com.ykt.model.Address;
import com.ykt.model.Admin;
import com.ykt.model.AgentFee;
import com.ykt.model.BaseData;
import com.ykt.model.Camera;
import com.ykt.model.CameraMarket;
import com.ykt.model.CameraShop;
import com.ykt.model.Category;
import com.ykt.model.ClientVersion;
import com.ykt.model.Community;
import com.ykt.model.Goods;
import com.ykt.model.GoodsCategory;
import com.ykt.model.GoodsRecommend;
import com.ykt.model.GoodsUnit;
import com.ykt.model.LoginLog;
import com.ykt.model.Market;
import com.ykt.model.MediaSource;
import com.ykt.model.Menu;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.model.PayRecord;
import com.ykt.model.Role;
import com.ykt.model.Service;
import com.ykt.model.Shop;
import com.ykt.model.ShopCategory;
import com.ykt.model.User;
import com.ykt.model.UserAddress;
import com.ykt.model.UserFeedback;
import com.ykt.model.Video;

public class AdminConfig extends JFinalConfig {

    @Override
    public void configConstant(Constants me) {
        PropKit.use("config.properties"); // 加载配置文件
        
        me.setMaxPostSize(1024 * 1024 * 50);

    	me.setDevMode(true);
        me.setBaseViewPath("/template");
    }

    @Override
    public void configRoute(Routes me) {
        AutoBindRoutes ab = new AutoBindRoutes();
        ab.autoScan(false);
        me.add(ab);
    }

    @Override
    public void configPlugin(Plugins me) {
        // DB连接池插件
        Map<String, String> conf = ResourceKit.readProperties("jdbc.properties");
        C3p0Plugin c3p0Plugin = new C3p0Plugin(conf.get("jdbcUrl"), conf.get("user"), conf.get("password").trim());
        me.add(c3p0Plugin);


        // DB操作插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        arp.addMapping("admin", Admin.class);
        arp.addMapping("menu", Menu.class);
        arp.addMapping("role", Role.class);
        arp.addMapping("login_log", LoginLog.class);

        arp.addMapping("shop", Shop.class);
        arp.addMapping("user", User.class);
        arp.addMapping("user_address", UserAddress.class);
        arp.addMapping("camera", Camera.class);
        arp.addMapping("camera_shop", CameraShop.class);
        arp.addMapping("market", Market.class);
        arp.addMapping("camera_market", CameraMarket.class);
        arp.addMapping("category", Category.class);
        arp.addMapping("goods", Goods.class);
        arp.addMapping("goods_unit", GoodsUnit.class);
        arp.addMapping("order", Order.class);
        arp.addMapping("order_item", OrderItem.class);
        arp.addMapping("goods", Goods.class);
        arp.addMapping("user_feedback", UserFeedback.class);
        arp.addMapping("community", Community.class);
        arp.addMapping("client_version", ClientVersion.class);
        arp.addMapping("address", Address.class);
        arp.addMapping("pay_record", PayRecord.class);
        
        arp.addMapping("base_data", BaseData.class);
        arp.addMapping("service", Service.class);
        
        arp.addMapping("shop_category", ShopCategory.class);
        arp.addMapping("goods_category", GoodsCategory.class);
        arp.addMapping("goods_recommend", GoodsRecommend.class);
        arp.addMapping("ad", Ad.class);
        arp.addMapping("video", Video.class);
        
        arp.addMapping("media_source", MediaSource.class);
        arp.addMapping("agent_fee", AgentFee.class);

        arp.setDevMode(true);
        arp.setShowSql(true);
        me.add(arp);


        // 定时任务
        Cron4jPlugin cp = new Cron4jPlugin();
        cp.config("job.properties");
        me.add(cp);

    }

    @Override
    public void configInterceptor(Interceptors me) {
    	me.add(new LoginAdminInterceptor());
		me.add(new AuthorityIntercept());
		me.add(new SessionInViewInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {

    }
}
